package com.chushouya.manager.service.admin.impl;

import com.general.framework.core.exception.Ex;
import com.general.framework.core.schema.PageResult;
import com.chushouya.manager.dao.entity.UserEntity;
import com.chushouya.manager.dao.repository.UserRepository;
import com.chushouya.manager.dto.admin.user.UserListDTO;
import com.chushouya.manager.dto.admin.user.query.UserQuery;
import com.chushouya.manager.service.admin.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public PageResult<UserListDTO> selectUserPageResult(UserQuery query) {
        PageResult<UserEntity> pageResult = userRepository.selectUserPageResult(query);
        return PageResult.of(pageResult, UserListDTO.class);
    }

    @Override
    public void toggleStatus(Long userId, Integer status) {
        UserEntity userEntity = userRepository.selectByPrimaryKey(userId);
        if(Objects.isNull(userEntity)){
            throw Ex.business("用户不存在");
        }
        if(userEntity.getStatus().equals(status)){
            throw Ex.business("用户状态未变更");
        }
        UserEntity update = new UserEntity();
        update.setUserId(userId);
        update.setStatus(status);
        userRepository.updateByPrimaryKeySelective(update);
    }
}
